var mainApp = angular.module("mainApp");
mainApp.controller("userController", function($scope, $http) {
	var um = $scope.um = {
		pager : {},
		condition : {}
	};

	um.query = function() {
		var data = {
			username : um.condition.username,
			pageSize : um.pager.pageSize,
			pageNum : um.pager.pageNum
		}
		$http.post("system/user/list.do", data).success(function(data) {
			um.pager = data.pager;
		});
	}
	um.query();

	um.clear = function() {
		um.condition = {};
		um.query();
	};

	um.edit = function(user) {
		um.title = "用户修改";
		um.user = angular.copy(user);
		um.roleSelected = um.user.roleList;
	}

	um.addOrEdit = function(form) {
		var ids = "";
		um.roleSelected.forEach(function(role) {
			ids = ids + role.id + ",";
		})
		$http.post("system/user/editRole.do", {
			userid : um.user.id,
			roleids : ids
		}).success(function(data) {
			um.showMessage = true;
			um.result = data.result;
			if (data.result == 1) {
				um.message = '操作成功!';
			} else {
				um.message = '操作失败！';
			}
		})
	}

	$("#userDetail").on("show.bs.modal", function() {
		$http.post("system/role/list.do").success(function(data) {
			um.roleList = data.roleList;
		});
	});

	$("#userDetail").on("hidden.bs.modal", function() {
		um.user = {};
		um.roleSelected = {};
		um.showMessage = false;
		um.query();
	});
})